<?php
namespace app\index\controller;

use app\common\controller\Common;
use org\Page;
use think\Exception;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Request;

class Category extends Common
{
    protected function initialize()
    {
        parent::initialize();

    }

    public function index($where = '')
    {
        $current_update_time = 0;
        $current_status = 2;
        $page = 1;  //页码
        $page_size = 30; //页码大小

        $where_arr = array();
        if (empty($where)) {
            $where_arr[0] = 'all';
        }else {
            $where_arr = explode("_",$where);
        }

        if (count($where_arr) > 1) {
            $current_update_time = $where_arr[1];
        }
        if (count($where_arr) > 2) {
            $current_status = $where_arr[2];
        }
        if (count($where_arr) > 3) {
            $page = $where_arr[3];
        }

        //分类
        $classify_arr = array('all' => array('id' => 0,'alias' => 'all','name' => '全部','path' => '/classify/all'));
        foreach ($this->site_config['categorys'] as $item) {
            $classify_arr[$item['alias']] = array(
                'id' => $item['id'],
                'alias' => $item['alias'],
                'name' => $item['name'],
                'path' => '/classify/' . $item['alias']
            );
        }
        //时间选择
        $classify_update_times = array(array('time' => 0,'name' => "不限"), array('time' => 1,'name' => "今天"), array('time' => 3,'name' => "三日内"), array('time' => 7,'name' => "七日内"), array('time' => 15,'name' => "半月内"), array('time' => 30,'name' => "一月内"));
        foreach ($classify_update_times as $key => $item) {
            $classify_update_times[$key]['path'] = sprintf("%s_%d_%d",$classify_arr[$where_arr[0]]['path'],$item['time'],$current_status);
        }
        //连载状态
        $classify_status = array(array('status' => 2,'name' => "不限"), array('status' => 0,'name' => "连载中"), array('status' => 1,'name' => "已完结"));
        foreach ($classify_status as $key => $item) {
            $classify_status[$key]['path'] = sprintf("%s_%d_%d",$classify_arr[$where_arr[0]]['path'],$current_update_time,$item['status']);
        }

        //当前分类
        $current_classify = $classify_arr[$where_arr[0]];
        $data = curl_server(sprintf("apiv2/classifylist?time_type=%d&full_type=%d&cid=%d&page=%d&page_size=%d",$current_update_time,$current_status,$current_classify['id'],$page,$page_size));
        $data = $data['data'];

        //分页
        $pages = new Page(sprintf("classify/%s_%d_%d_{PAGE}",$where_arr[0],$current_update_time,$current_status),$page,$data['total'],$page_size);
        if (isMobileDomain()) {
            $pages->setConfig("theme",'%NO_PREV% %UP_PAGE% %DOWN_PAGE%');
            $show = $pages->showHs13Mobile();
        }else {
            $show = $pages->show();
        }

        $this->assign("pages",$show);

        $this->assign("current_status",$current_status);
        $this->assign("current_update_time",$current_update_time);
        $this->assign("classify_status",$classify_status);
        $this->assign("classify_update_times",$classify_update_times);
        $this->assign("current_alias",$where_arr[0]);

        $this->site_seo('category',array('category_name' => $current_classify['name']));
        return $this->fetch("index",['classify' => $classify_arr,'current_classify' => $current_classify,'list' => $data['list']]);
    }

}

